package com.xhwl.logistics.service.impl.redis;

import com.xhwl.logistics.util.AboutDateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.TimeUnit;

//@Service
public class VehicleLogRedisServiceImpl {

    private final String vehicleLogPrefix = "cent-logistics:log:";

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Resource(name = "redisTemplateForObject")
    private ValueOperations<String, Object> valueOperations;

    public void log(String key, String value) {
        stringRedisTemplate.opsForValue().set(vehicleLogPrefix+key, value);
    }

    public void log(String key, Object object) {
        valueOperations.set(vehicleLogPrefix+key, object);
    }

    public void logWithTime(String key, Object object) {
        valueOperations.set(vehicleLogPrefix+key+":" + AboutDateUtil.dateToString(new Date(), "yyyy-MM-dd'T'HHmmss.SSS"), object);
    }

    public void logWithTime(String key, Object object, Long expireTime, TimeUnit timeUnit) {
        valueOperations.set(vehicleLogPrefix+key+":" + AboutDateUtil.dateToString(new Date(), "yyyy-MM-dd'T'HHmmss.SSS"), object, expireTime, timeUnit);
    }

}
